# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openimagedenoise
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.0
pkgrel=3
pkgdesc="Intel Open Image Denoise library of high-performance, high-quality denoising filters for images rendered with ray tracing (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://openimagedenoise.github.io/"
license=("spdx:Apache-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-tbb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "git")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/OpenImageDenoise/oidn/releases/download/v${pkgver}/oidn-${pkgver}.src.tar.gz
        001-build-fixes.patch
        002-Fix-building-with-TBB-2021.patch
        003-use-the-same-_cpuid-as-msvc.patch)
sha256sums=('4dd484abea8a0b3d12d346343fcb1ab7abef8f94318d8c537f69a20c2a75c4eb'
            'e789c3ef6be360ec42f6b0da805bc424f0e021b093091361d2be53a77946724d'
            'ccc7619319e7fef7d3b476e1247e15615ab5441a23d0145f46d305a24fcc9be8'
            '372c36b5c1ee7ff6472d4e750aeaa92828a2d71de48784a00fef7743136434c2')

prepare() {
  cd "${srcdir}"/oidn-${pkgver}
  patch -p1 -i "${srcdir}"/001-build-fixes.patch
  patch -p1 -i "${srcdir}"/002-Fix-building-with-TBB-2021.patch
  patch -p1 -i "${srcdir}"/003-use-the-same-_cpuid-as-msvc.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DTBB_INCLUDE_DIR=${MINGW_PREFIX}/include \
    -DTBB_LIBRARY=${MINGW_PREFIX}/lib/libtbb12.dll.a \
    -DTBB_LIBRARY_MALLOC=${MINGW_PREFIX}/lib/libtbbmalloc.dll.a \
    ../oidn-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/oidn-${pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
